package com.mryin.performance.utils;


import com.mryin.performance.common.Constant;
import com.mryin.performance.mapper.TaskMapper;
import com.mryin.performance.mapper.UserMapper;
import com.mryin.performance.pojo.Task;
import com.mryin.performance.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: PerformanceManager
 * @description: 绩效计算类
 * @author: Mr.YIN
 * @create: 2021-03-04 15:42
 **/
public class PerformanceUtils implements Serializable {



    public static Double inCome(Integer monovalentSum , Double salary , Integer workday , Integer actualworkday){
        return (monovalentSum+salary)/workday * actualworkday ;
    }

    public static Double surplusValue(Integer marketpriceSum , Double inCome , Integer workhour){
        return (marketpriceSum - inCome)/workhour;
    }

    public static Integer valueComparatorBySurplusValue(String name ,
                                          UserMapper userMapper,
                                          Integer monovalentSum,
                                          Integer marketpriceSum,
                                          TaskMapper taskMapper){
        Integer num = 1;
        HashMap<String, Double> map = new HashMap<String, Double>();
        List<User> users = userMapper.selectList(null);
        for (User user : users) {
            List<Task> allFinishTask = taskMapper.getAllFinishTask(user.getName());
            for (Task task : allFinishTask) {
                monovalentSum += task.getMonovalent();
                marketpriceSum += task.getMarketprice();
            }
            Double income = inCome(monovalentSum, user.getSalary(), Constant.WORK_DAY, taskMapper.getActualWorkDay());
            Double surplusValue = surplusValue(marketpriceSum, income, (Constant.WORK_HOUR * taskMapper.getActualWorkDay()));
            map.put(user.getName(), surplusValue);
            monovalentSum = 0 ;
            marketpriceSum = 0 ;
        }
        Map<String, Double> stringDoubleMap = ValueComparator.sortByValues(map);
        Set<Map.Entry<String, Double>> entries = stringDoubleMap.entrySet();
        entries.forEach(System.out::println);
        for (Map.Entry<String, Double> entry : entries) {
            if(entry.getKey().equals(name)){
                return num ;
            }
            num ++;
        }
        return num;
    }


    public static Integer valueComparatorByInCome(String name ,
                                                        UserMapper userMapper,
                                                        Integer monovalentSum,
                                                        TaskMapper taskMapper){
        Integer num = 1;
        HashMap<String, Double> map = new HashMap<String, Double>();
        List<User> users = userMapper.selectList(null);
        for (User user : users) {
            List<Task> allFinishTask = taskMapper.getAllFinishTask(user.getName());
            for (Task task : allFinishTask) {
                monovalentSum += task.getMonovalent();
            }
            Double income = inCome(monovalentSum, user.getSalary(), Constant.WORK_DAY, taskMapper.getActualWorkDay());
            map.put(user.getName(), income);
            monovalentSum = 0 ;
        }
        Map<String, Double> stringDoubleMap = ValueComparator.sortByValues(map);
        Set<Map.Entry<String, Double>> entries = stringDoubleMap.entrySet();
        entries.forEach(System.out::println);
        for (Map.Entry<String, Double> entry : entries) {
            if(entry.getKey().equals(name)){
                return num ;
            }
            num ++;
        }
        return num;
    }

}
